// @author Tjzlo
// @version 2025/2/6 00:58

package com.old.controller;

import com.old.dto.CommentDTO;
import com.old.dto.CommentQueryDTO;
import com.old.result.PageBean;
import com.old.result.PageResult;
import com.old.result.Result;
import com.old.service.CommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
@RequestMapping("/home")
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;

    /**
     * 根据文章id查询评论
     * @param commentQueryDTO
     * @return
     */

    @GetMapping("/getCommentList")
    public Result<PageBean> getCommentList(CommentQueryDTO commentQueryDTO) {
        //前置判定是否存在帖子id
        if (commentQueryDTO.getPostId() == null) {
            log.info("未接收到帖子id,请检查请求参数");
            return Result.error("未接收到帖子id,请检查请求参数");
        }else{
            //获取评论列表
            log.info("{}号帖子分页查询评论,参数:{}",commentQueryDTO.getPostId(),commentQueryDTO);
            if (commentQueryDTO.getPageSize() == null) {
                log.info("未接收到每页容量,设置为10");
                commentQueryDTO.setPageSize(10);
            }
            if (commentQueryDTO.getPage() == null) {
                log.info("未接收到页码,设置为1");
                commentQueryDTO.setPage(1);
            }
            PageBean pageResult = commentService.commentListPage(commentQueryDTO);
            log.info("{}号帖子评论查询成功,共{}条,评论内容为:{}",commentQueryDTO.getPostId(),pageResult.getTotal(),pageResult.getList());
            return Result.success(pageResult);
        }
    }

    /**
     * 根据文章id发表评论
     * @param commentGet
     * @return
     */
    @PostMapping("/addComment")
    public Result<CommentDTO> addComment(CommentDTO commentGet) {
        //添加评论
        log.info("{}用户评论{}帖子", commentGet.getCommentAuthorId(), commentGet.getPostId());
        commentService.addComment(commentGet);
        log.info("{}用户评论{}帖子成功", commentGet.getCommentAuthorId(), commentGet.getPostId());
        return Result.success();
    }

}
